package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.*;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.SysCruiseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @description 针对表【sys_cruise_device(巡航组合信息表)】的数据库操作Service实现
 * @createDate 2022-07-30 21:37:29
 */
@Service
public class SysCruiseServiceImpl extends ServiceImpl<SysCruiseMapper, SysCruise> implements SysCruiseService {

    @Autowired
    SysCruiseMapper sysCruiseMapper;

    @Autowired
    SysCruiseDeviceMapper sysCruiseDeviceMapper;

    @Autowired
    SysDeviceInfoMapper sysDeviceInfoMapper;

    @Autowired
    SysDeviceTypeMapper sysDeviceTypeMapper;

    @Autowired
    SysMonitorDeviceMapper sysMonitorDeviceMapper;

    @Override
    public AjaxResult getCruiseList(SysCruise sysCruise) {
        List<SysCruise> sysCruises = sysCruiseMapper.selectList(Wrappers.<SysCruise>lambdaQuery()
                .eq(SysCruise::getState, 1)
                .eq(Objects.nonNull(sysCruise.getId()), SysCruise::getId, sysCruise.getId())
                .eq(Objects.nonNull(sysCruise.getCruiseType()) && sysCruise.getCruiseType().intValue() != 0, SysCruise::getCruiseType,sysCruise.getCruiseType())
        );
        if (!sysCruises.isEmpty()) {
            sysCruises.forEach(v -> {
                List<SysCruiseDevice> sysCruiseDevices = sysCruiseDeviceMapper.selectList(Wrappers.<SysCruiseDevice>lambdaQuery().eq(SysCruiseDevice::getCruiseId, v.getId()).eq(SysCruiseDevice::getState, 1));
                if (!sysCruiseDevices.isEmpty()) {
                    ArrayList<SysDeviceInfo> sysDeviceInfos = new ArrayList<>();
                    sysCruiseDevices.forEach(s -> {
                        SysDeviceInfo sysDeviceInfo = sysDeviceInfoMapper.selectOne(Wrappers.<SysDeviceInfo>lambdaQuery().eq(SysDeviceInfo::getState, 1).eq(SysDeviceInfo::getId, s.getDeviceId()));
                        if (Objects.nonNull(sysDeviceInfo)) {
                            SysDeviceType sysDeviceType = sysDeviceTypeMapper.selectOne(Wrappers.<SysDeviceType>lambdaQuery().eq(SysDeviceType::getDeviceType, sysDeviceInfo.getDeviceType()));
                            sysDeviceInfo.setDeviceTypeName(sysDeviceType.getName());
                        }
                        sysDeviceInfos.add(sysDeviceInfo);
                    });
                    v.setDevicelist(sysDeviceInfos);
                }
            });
        }
        return AjaxResult.success(sysCruises);
    }

    @Override
    public AjaxResult deleteCruiseList(SysCruise sysCruise) {
        SysCruise sysCruiseNew = new SysCruise();
        sysCruiseNew.setId(sysCruise.getId());
        sysCruiseNew.setState(0);

        sysCruiseMapper.updateById(sysCruiseNew);

        SysCruiseDevice sysCruiseDevice = new SysCruiseDevice();
        sysCruiseDevice.setState(0);
        sysCruiseDeviceMapper.update(sysCruiseDevice, Wrappers.<SysCruiseDevice>lambdaQuery().eq(SysCruiseDevice::getId, sysCruise.getId()));

        return AjaxResult.success();
    }

    @Override
    public AjaxResult getCruiseDevice() {
        ArrayList<SysDeviceInfo> sysDeviceInfosNew = new ArrayList<>();
        List<SysDeviceInfo> sysDeviceInfos = sysDeviceInfoMapper.selectList(Wrappers.<SysDeviceInfo>lambdaQuery().eq(SysDeviceInfo::getState, 1));
        if (!sysDeviceInfos.isEmpty()) {
            sysDeviceInfos.forEach(v -> {
                Integer id = v.getId();
                SysCruiseDevice sysCruiseDevice = sysCruiseDeviceMapper.selectOne(Wrappers.<SysCruiseDevice>lambdaQuery().eq(SysCruiseDevice::getDeviceId, id).eq(SysCruiseDevice::getState, 1));
                SysMonitorDevice sysMonitorDevice = sysMonitorDeviceMapper.selectOne(Wrappers.<SysMonitorDevice>lambdaQuery().eq(SysMonitorDevice::getDeviceId, id).eq(SysMonitorDevice::getState, 1));
                if (Objects.isNull(sysCruiseDevice) && Objects.isNull(sysMonitorDevice)) {
                    SysDeviceType sysDeviceType = sysDeviceTypeMapper.selectOne(Wrappers.<SysDeviceType>lambdaQuery().eq(SysDeviceType::getDeviceType, v.getDeviceType()));
                    v.setDeviceTypeName(sysDeviceType.getName());
                    sysDeviceInfosNew.add(v);
                }
            });
        }
        return AjaxResult.success(sysDeviceInfosNew);
    }

    @Override
    public AjaxResult addCruiseDevice(SysCruise sysCruise) {
        SysCruiseDevice sysCruiseDevice = new SysCruiseDevice();
        sysCruiseDevice.setState(0);
        sysCruiseDeviceMapper.update(sysCruiseDevice, Wrappers.<SysCruiseDevice>lambdaQuery().eq(SysCruiseDevice::getDeviceId, sysCruise.getId()));

        List<SysDeviceInfo> list = sysCruise.getDevicelist();
        if (!list.isEmpty()) {
            Date nowDate = DateUtils.getNowDate();
            list.forEach(v -> {
                SysCruiseDevice sysCruiseDeviceNew = new SysCruiseDevice();
                sysCruiseDeviceNew.setCruiseId(sysCruise.getId());
                sysCruiseDeviceNew.setDeviceId(v.getId());
                sysCruiseDeviceNew.setCreateTime(nowDate);
                sysCruiseDeviceNew.setState(1);
                sysCruiseDeviceMapper.insert(sysCruiseDeviceNew);
                }
            );
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult addCruise(SysCruise sysCruise) {
        SysCruise cruise = new SysCruise();
        cruise.setCruiseName(sysCruise.getCruiseName());
        cruise.setCreatedTime(DateUtils.getNowDate());
        cruise.setState(1);
        sysCruiseMapper.insert(cruise);
        return AjaxResult.success();
    }
}




